﻿
//==============================================================================
// 文件     ：byteExt.cs
// 功能概要 ：byte的扩展方法
//           
//           
// 作者    ：周薇
// 时间    ：2011年5月16日
// Copyright (c) 2011 重庆安运科技公司. All rights reserved.
//==============================================================================

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Webcar.Utils
{
    public static class byteExt
    {
        /// <summary>
        /// xor
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static byte Xor(this  byte[] bytes)
        {
            if (bytes.Length == 0) return 0;
            int s1 = bytes[0];
            for (int i = 1; i < bytes.Length; i++)
            {
                s1 = s1 ^ bytes[i];
            }
            return (byte)s1;
        }
        /// <summary>
        /// byte[]转换为utf-8 string
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static string ToUTF8String(this byte[] bytes)
        {
            return Encoding.UTF8.GetString(bytes, 0, bytes.Length).Trim();
        }
        /// <summary>
        /// 转换为16进制字符串
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static string ToHexString(this byte[] bytes)
        {
            return BitConverter.ToString(bytes).Replace("-", "");
        }
        public static void Save(this byte[] bytes,string file)
        {
            File.WriteAllBytes(file, bytes);
        }
        public static string ToBcdString(this byte[] bytes)
        {
            StringBuilder sb = new StringBuilder();
            foreach (byte b in bytes)
            {
                string s = b.ToString("00");
                if (s.Length > 2)
                {
                    throw new ApplicationException("ToBcdString函数溢出，参数为" + bytes.ToHexString());
                }
                sb.Append(s);
            }
            return sb.ToString();
        }

        /// <summary>
        /// byte[]转换为16进制字符串
        /// </summary>
        /// <returns></returns>
        public static string ToHexString(this byte[] bytes,int start,int len)
        {
            return BitConverter.ToString(bytes, start,len).Replace("-", "");
        }
        /// <summary>
        /// 转换为16进制
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static string ToHexString(this byte bytes)
        {
            return bytes.ToString("X2");
        }
        /// <summary>
        /// 和字符串的PadLeft相同
        /// </summary>
        /// <param name="bytes"></param>
        /// <param name="len"></param>
        /// <returns></returns>
        public static byte[] PadLeft(this byte[] bytes, int len)
        {
            if (bytes.Length >= len) return bytes;
            int padlen = len - bytes.Length;
            return ArrayHelper.ArrayMerge(new byte[padlen], bytes);
        }
        /// <summary>
        /// 和字符串的PadRight相同
        /// </summary>
        /// <param name="bytes"></param>
        /// <param name="len"></param>
        /// <returns></returns>
        public static byte[] PadRight(this byte[] bytes, int len)
        {
            if (bytes.Length >= len) return bytes;
            int padlen = len - bytes.Length;
            return ArrayHelper.ArrayMerge(bytes, new byte[padlen]);
        }

        
    }
}
